package com.tma.basicfragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;

import com.tma.basicfragment.CategoryFragment.OnCategorySelectedListener;

public class MainActivity extends FragmentActivity implements
        OnCategorySelectedListener {
        //LoadBookTask.OnTaskCompletedListener,
        //LoadSongTask.OnTaskCompletedListener {

    static final String TAG = "MainActivity";
    
    private CategoryDetailFragment mDispFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate");

        // Check whether the activity is using the layout version with
        // the f_container FrameLayout. If so, we must add the first fragment
        if (findViewById(R.id.f_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState == null) {
                CategoryFragment firstFragment = new CategoryFragment();

                // In case this activity was started with special instructions from an Intent,
                // pass the Intent's extras to the fragment as arguments
                firstFragment.setArguments(getIntent().getExtras());

                // Add the fragment to the 'f_container' FrameLayout
                getSupportFragmentManager()
                        .beginTransaction()
                        .add(R.id.f_container, firstFragment)
                        .commit();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onCategorySelected(int position) {
        CategoryDetailFragment detailFragment = (CategoryDetailFragment)
                getSupportFragmentManager().findFragmentById(R.id.f_category_detail);

        if (detailFragment != null) {
            mDispFragment = detailFragment;
            // delegate event to target Fragment
            mDispFragment.onCategorySelected(position);
        } else {
            // If the detail fragment is not available, we're in the one-pane layout and must swap frags...
            // Create fragment and give it an argument for the selected article
            CategoryDetailFragment newFragment = new CategoryDetailFragment();
            Bundle args = new Bundle();
            args.putInt(CategoryDetailFragment.KEY_POSITION, position);
            newFragment.setArguments(args);

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.f_container, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();

            mDispFragment = newFragment;
        }
    }
}
